package com.webside.points.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.alibaba.fastjson.JSON;
import com.webside.points.model.PostEntity;
import com.webside.points.model.PostTypeEntity;
import com.webside.points.model.UserEntity;
import com.webside.points.service.PostService;
import com.webside.points.service.PostTypeService;
import com.webside.points.service.UserService;
import com.webside.points.wechat.PageUtil;
import com.webside.points.wechat.UploadUtil;
@Controller
@Scope("prototype")
@RequestMapping("/postType/")
public class PostTypeController {
	
	@Autowired
	PostTypeService postTypeService;
	
	@Autowired
	PostService postService;
	
	@Autowired
	UserService userService;
	
	
	@RequestMapping("findAllUI")
	public String findAllUI(Model model){
		
		System.out.println("进入文章类型查询：>>>");
		PageUtil pageUtil=new PageUtil();
		PostTypeEntity postTypeEntity=new PostTypeEntity();
		int totalCount=postTypeService.findCount(postTypeEntity);
		pageUtil.setTotalCount(totalCount);
		pageUtil.setCurPage(1);
		model.addAttribute("pageUtil", pageUtil);
		
		return "serve/postType";
	}
	
	@RequestMapping("findAll")
	public void findAll(Model model,PostTypeEntity postTypeEntity,HttpServletResponse response){
		System.out.println("进入查询文章类型：>>>"+postTypeEntity);
		 PageUtil pageUtil=new PageUtil();
		 
		 if(String.valueOf(postTypeEntity.getCurPage())!=null){
			 int cPage=Integer.valueOf(postTypeEntity.getCurPage());
			 cPage=(cPage-1)*10;
			 postTypeEntity.setCurPage(Integer.valueOf(cPage));
		 }
		 postTypeEntity.setName("%"+postTypeEntity.getName()+"%");
		 
		 List<PostTypeEntity> list=postTypeService.findAll(postTypeEntity);
		 System.out.println(list);
		  pageUtil.setContent(list);
		  model.addAttribute("pageUtil", pageUtil);
		  
		  String json = JSON.toJSONString(pageUtil);
			try {
				response.getWriter().write(json);
				response.flushBuffer();
			} catch (Exception e) {
				System.out.println("JsonError："+e);
			}
	}
	
	@RequestMapping("insert")
	public String insert(PostTypeEntity postTypeEntity,@RequestParam("file") CommonsMultipartFile file,HttpServletRequest request){
		
		postTypeEntity.setStatus("1");
		postTypeEntity.setCreateTime(new Date(System.currentTimeMillis()));
		System.out.println("进入文章类型增加：>>>"+postTypeEntity +"  "+file);
		//上传图片
		String image = UploadUtil.uploadImage(file, request);
		if(image!=null){
			postTypeEntity.setImage(image);
			postTypeService.insert(postTypeEntity);
		}
		return "redirect:findAllUI";
	}
	
	@RequestMapping("delete")
	public String delete(int id){
		PostTypeEntity postTypeEntity = postTypeService.findById(id);
		if(postTypeEntity != null){
			UploadUtil.removeImage(postTypeEntity.getImage());
		}
		System.out.println(postTypeService.delete(id));
		return "redirect:findAllUI";
	}
	
	@RequestMapping("update")
	public String update(PostTypeEntity postTypeEntity,@RequestParam("file") CommonsMultipartFile file,HttpServletRequest request){
		System.out.println("进入课程管理修改：>>>"+postTypeEntity+"  "+file);
		//上传图片
		String image = UploadUtil.uploadImage(file, request);
		if(image!=null){
			postTypeEntity.setImage(image);
			System.out.println(postTypeService.update(postTypeEntity));
		}
		return "redirect:findAllUI";
	}
	
	@RequestMapping("findById")
	public String findById(int id,Model model){
		
		model.addAttribute("postType",postTypeService.findById(id));
		return "serve/postTypeForm";
	}
	
	@RequestMapping("findPostType")
	public void findPostType(HttpServletResponse response){
	
		List<PostTypeEntity> list = postTypeService.findPostType("");
		
		System.out.println("文章类型list：>>>"+list);
		
		String json = JSON.toJSONString(list);
		try {
			response.getWriter().write(json);
			response.flushBuffer();
		} catch (Exception e) {
			System.out.println("JsonError："+e);
		}
	}
	
	@RequestMapping("findAllPostType")
	public String findAllPostType(String name,Model model){
		
		if(name!=null){
			try {
				name=URLDecoder.decode(name,"utf-8");
				name="%"+name+"%";
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
	
		List<PostTypeEntity> list = postTypeService.findPostType(name);
		
		System.out.println("文章类型list：>>>"+list);
		
		model.addAttribute("postTypeList", list);
		
		return "service/post-type";
	}
	
	@RequestMapping("postTypeIntroduce")
	public String postTypeIntroduce(int postTypeId,Model model,HttpServletRequest request){
		
		PostTypeEntity postTypeEntity = postTypeService.findById(postTypeId);
		request.getSession().setAttribute("postType", postTypeEntity);
		
		List<PostEntity> postList = postService.findByPostTypeId(postTypeId);
		
		if(postList.size()>1){
			request.getSession().setAttribute("post", postList.get(0));
		}else{
			PostEntity postEntity =  new PostEntity();
			postEntity.setId(0);
			request.getSession().setAttribute("post", postEntity);
		}
		
		String openId = (String) request.getSession().getAttribute("openId");
		if(openId != null){
			UserEntity userEntity = userService.findByOpenId(openId);
			request.getSession().setAttribute("user", userEntity);
		}
		
		return "redirect:../service/post-introduce.jsp";
	}
	@RequestMapping("findInfo")
	public String findInfo(int postTypeId,Model model){
		PostTypeEntity postTypeEntity = postTypeService.findById(postTypeId);
		
		model.addAttribute("postType", postTypeEntity);
		return "serve/postTypeInfo";
	}

}
